package pt.uma.gap.ast;


import org.eclipse.jdt.core.dom.Assignment;


public class EvaluateAssignment {
	
	// My comment 
	
	public static void evaluate(AssignmentInfo assignmentInfo){
		
		if(!assignmentInfo.getAssignmentType().equals(FlowType.UNDEFINED) ){
			return;
		}
		// to check += or -=, if this is the case, we can decide it is Value_FLOW
		else if(!isEqualOperator(assignmentInfo)){
			assignmentInfo.setAssignmentType(FlowType.VALUE_FLOW_RULE);
		}
		// check if it is primitive type on L.H.S (such as int x=10), it is understood, it is vlaue.flow 
		else if(isPrimitive(assignmentInfo)){
			assignmentInfo.setAssignmentType(FlowType.VALUE_FLOW_RULE);
			// possibility of ADDRESS_FLOW
		}
		
		/* commented by Ijaz--- check it 
		else if(assignmentInfo.getAssignment().getLeftHandSide().toString().replace("this.", "")
				.equals(assignmentInfo.getAssignment().getRightHandSide().toString().replace("this.", ""))){
			assignmentInfo.setAssignmentType(FlowType.SELF_ADDRESS_FLOW);
		}*/
		else{
			assignmentInfo.setAssignmentType(FlowType.ADDRESS_FLOW_RULE);
		}
		
	}
	
	
	/**
	 * @param assignmentInfo
	 * @return
	 */
	private static boolean isEqualOperator(AssignmentInfo assignmentInfo) {
		
		Assignment assign=assignmentInfo.getAssignment();
		// to check += or -=, if this is the case, we can decide it is Value_FLOW
		return assign.getOperator().equals(Assignment.Operator.ASSIGN);
	}

	private static boolean isPrimitive(AssignmentInfo assignmentInfo) {
		Assignment assign=assignmentInfo.getAssignment();
		return assign.resolveTypeBinding().isPrimitive();
	}

}
